package homework12月9号补交.homework2.d10;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * @Version 1.0
 * @Author:zhaoJiaCai
 * @Date:2019/12/8 星期日   20:06
 */
public class Test {
    public static void main(String[] args) throws Exception{

        Object run = invokeMethod(new DemoClass(), "run", new String[]{"1"});

        System.out.println(run);
    }

    /**
     * 方法功能：执行某对象方法
     * @param obj 对象
     * @param methodName 方法名
     * @param args 参数数组
     * @return 方法返回值
     */
    public static Object invokeMethod(Object obj, String methodName, Object[] args)  throws Exception{
        Class<?> aClass = obj.getClass();
        DemoClass demoClass = (DemoClass) aClass.newInstance();
        ArrayList<Class<? extends String>> arr = new ArrayList<>();
        for (Object arg : args) {
            System.out.println(arg.getClass().toString());
            arr.add(arg.getClass().toString().getClass());
            Class<? extends String> aClass1 = arg.getClass().toString().getClass();
        }
        //将集合 数据 转移到数组
        Class[] classes = arr.toArray(new Class[0]);
        Method method = aClass.getMethod(methodName, classes);
        Object invoke = method.invoke(demoClass, args);
        return invoke;
    }

}
